package com.zp.qualitypractice.list.header;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zp.qualitypractice.R;
import com.zp.qualitypractice.list.CustomCircleView;

/**
 * Created by izp on 2017/4/20.
 */

public abstract class BaseRefreshHeader extends LinearLayout {
    protected LinearLayout mHeaderViewContainer;
    protected TextView mHeaderText;
    protected CustomCircleView mHeaderCircle;
    protected ImageView mHeaderArrow;
    protected ProgressBar mProgressBar;

    public BaseRefreshHeader(Context context) {
        super(context);
        init(context);
    }

    public BaseRefreshHeader(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BaseRefreshHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BaseRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        mHeaderViewContainer = getHeaderContainerView(context);
        mHeaderText = (TextView) mHeaderViewContainer.findViewById(R.id.header_text);
        mHeaderCircle = (CustomCircleView) mHeaderViewContainer.findViewById(R.id.custom_header_circle);
        mHeaderArrow = (ImageView) mHeaderViewContainer.findViewById(R.id.header_arrow);
        mProgressBar = (ProgressBar) mHeaderViewContainer.findViewById(R.id.header_loading);

        initOther();
    }
    protected LinearLayout getHeaderContainerView(Context context) {
        return (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_list_refresh_header, null, true);
        //        return (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_list_refresh_header,null,false);
    }


    public void updateText(String text) {
        mHeaderText.setText(text);
    }

    public void startLoading() {
        mHeaderCircle.setVisibility(GONE);
        mHeaderArrow.setVisibility(GONE);
        mProgressBar.setVisibility(VISIBLE);
    }

    public void stopLoading() {
        mProgressBar.setVisibility(GONE);
        mHeaderCircle.setVisibility(VISIBLE);
        mHeaderArrow.setVisibility(VISIBLE);
    }

    public abstract void initOther();

    public abstract void updateCircle(float percent);
}
